//
//  HZTextView.swift
//  weibo002
//
//  Created by 黑眼圈 on 16/4/25.
//  Copyright © 2016年 heiyan. All rights reserved.
//

import UIKit

class HZTextView: UITextView {

   
    //提供给外界数组占位文字
    var placeholder:String? {
        
        didSet{
            
            label.text = placeholder
            
        }
    }

    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        
        
        //开启用户交互
//        userInteractionEnabled = true
        
        setupUI()
        
    }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    
    //添加子控件
    private func setupUI(){
        
        
        addSubview(label)
      
        
        //约束
        label.snp_makeConstraints { (make) -> Void in
            
            make.leading.equalTo(5)
            make.top.equalTo(8)
            make.width.equalTo(self).offset(-10)
            
        }
        
           
        
        //通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "textDidChage:", name: UITextViewTextDidChangeNotification, object: nil)
        
    }
    
    // MARK: -  实现通知的方法
    @objc func textDidChage(noti:NSNotification) {
        
        //当有文字的时候就隐藏占位文字
        label.hidden = hasText()
        

        
    }
    
    
    //销毁时移除通知中心
    deinit{
       NSNotificationCenter.defaultCenter().removeObserver(self)
        
    }
    
    
    
    //懒加载子控件
    lazy var label:UILabel = {
        
        let label = UILabel()
        
        
        label.font = UIFont.systemFontOfSize(12)
        label.text = "明天又是一个大晴天把"
        label.textColor = UIColor.lightGrayColor()
        
        
        return label
    }()
    

    
    
}
